Read in ‘greywolf_GBIF.csv’

wolf_df <- read.csv(here("data","greywolf_GBIF.csv")) %>% 
  clean_names() %>% 
  select(species, 
         country_code,
         locality,
         state_province,
         decimal_latitude,
         decimal_longitude,
         event_date,
         day,
         month,
         year,
         identified_by)

need to convert decimal_latitude and decimal_longitude to numeric values to then be able to convert to coordinates

wolf_df$decimal_latitude <- as.numeric(wolf_df$decimal_latitude)

wolf_df$decimal_longitude <- as.numeric(wolf_df$decimal_longitude)

# wolf_df <- wolf_df %>%
  #drop_na()

spatial wrangling bits

wolf_spatial = SpatialPoints(cbind(wolf_df$decimal_longitude, wolf_df$decimal_latitude), proj4string=CRS("+proj=longlat +datum=WGS84"))

st_crs(wolf_spatial)
## Coordinate Reference System:
##   User input: +proj=longlat +datum=WGS84 +no_defs 
##   wkt:
## GEOGCRS["unknown",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]],
##         ID["EPSG",6326]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433],
##         ID["EPSG",8901]],
##     CS[ellipsoidal,2],
##         AXIS["longitude",east,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433,
##                 ID["EPSG",9122]]],
##         AXIS["latitude",north,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433,
##                 ID["EPSG",9122]]]]
# then converting it to an sp and data frame
wolf_spatial_transform = spTransform(wolf_spatial, CRS("+proj=longlat +datum=WGS84")) %>%  as.data.frame()


# then cbinding it to the original wolf_df and removing our old "decimal_latitude" & "decimal_longitude" and renaming our new UTM coordinates
wolf_spatial_latlong<- cbind(wolf_df,wolf_spatial_transform) %>%
  rename(lat = coords.x1, long = coords.x2) %>%
  select(-decimal_latitude, -decimal_longitude)

## converting the lat long to crs 4326
wolf_spatial_cord <- st_as_sf(wolf_spatial_latlong, coords = c("lat", "long"), crs = 4326)

# st_crs(wolf_spatial_cord)

ggplot() +
  geom_sf(data = wolf_spatial_cord)

### interactive map

# world <- ne_countries(scale = "medium", returnclass = "sf")
# class(world)
# st_crs(world)
# world <- st_transform(world, st_crs(wolf_spatial_cord))
# ggplot(data = world) +
  geom_sf(data = wolf_spatial_cord)
## [[1]]
## mapping:  
## geom_sf: na.rm = FALSE
## stat_sf: na.rm = FALSE
## position_identity 
## 
## [[2]]
## <ggproto object: Class CoordSf, CoordCartesian, Coord, gg>
##     aspect: function
##     backtransform_range: function
##     clip: on
##     crs: NULL
##     datum: crs
##     default: TRUE
##     distance: function
##     expand: TRUE
##     fixup_graticule_labels: function
##     is_free: function
##     is_linear: function
##     label_axes: list
##     label_graticule: 
##     labels: function
##     limits: list
##     modify_scales: function
##     ndiscr: 100
##     range: function
##     render_axis_h: function
##     render_axis_v: function
##     render_bg: function
##     render_fg: function
##     setup_data: function
##     setup_layout: function
##     setup_panel_guides: function
##     setup_panel_params: function
##     setup_params: function
##     train_panel_guides: function
##     transform: function
##     super:  <ggproto object: Class CoordSf, CoordCartesian, Coord, gg>
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(wolf_spatial_cord) + 
  tm_dots()

summary stats

# wolf_df$country_code <- as.character(wolf_df$country_code)

wolf_sum <- wolf_df %>% 
    group_by(country_code) %>%
  summarize(n()) %>% 
  rename(wolf = 'n()')
## `summarise()` ungrouping output (override with `.groups` argument)
wolf_sum <- wolf_sum %>%  
mutate(country_code = fct_reorder(country_code, wolf))

ggplot(data = wolf_sum,
       aes(x = country_code, 
           y = wolf)) +
  geom_bar(stat="identity", fill="#D81313", alpha=.6, width=.4) +
    coord_flip() +
  theme_minimal() +
  labs(y = 'Count of Grey Wolves (Canis lupus) from 1700 to 2021',
       x = "Country Code")

annual comparison

wolf_count_year_country <- wolf_df %>% 
    group_by(year, country_code) %>%
  summarize(n()) %>% 
  rename(wolf = 'n()') %>% 
  drop_na() 
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(data = wolf_count_year_country,
       aes(x = year, 
           y = wolf,
           group = country_code)) +
  geom_line(alpha=.6, width=3, aes(color = country_code))  +
  theme_minimal() +
  labs(y = 'Count of Grey Wolves (Canis lupus)',
       x = "Year (1700 - 2021)") + 
  scale_color_brewer(palette="Dark2")
## Warning: Ignoring unknown parameters: width